﻿using System;
using System.Collections.Generic;
using System.Text;
using XiaoGang.Soft.XGWMS.DAL_SQL;
using System.Transactions;
using System.Data;
using System.Data.SqlClient;
using XiaoGang.Soft.XGWMS.MODEL;
using XiaoGang.Soft.XGWMS.COM;

namespace XiaoGang.Soft.XGWMS.BLL
{
    public class BLL_M007_GOODS
    {
        private readonly XiaoGang.Soft.XGWMS.DAL_SQL.DAL_M007_GOODS dal_GOODS = new XiaoGang.Soft.XGWMS.DAL_SQL.DAL_M007_GOODS();
        private readonly XiaoGang.Soft.XGWMS.DAL_SQL.DAL_ComProcess dal_ComProcess = new XiaoGang.Soft.XGWMS.DAL_SQL.DAL_ComProcess();
        private readonly XiaoGang.Soft.XGWMS.BLL.BLL_ComProcess bll_ComProcess = new BLL_ComProcess();

        /// <summary>
        /// <p><b>方法名称</b>GoodsDeleteCheck</p>	
        /// <p><b>概要説明  :</b>检测该商品有没有没使用</p>
        /// <p><b>作成者    :</b>万岗</p>	
        /// <p><b>作成日    :</b>2008/09/16</p>	
        /// <p><b>修改历史  :</b></p>	
        /// </summary>	
        /// <param type="string" name="strGoodsID">商品编码</param>
        /// <returns type = "ReturnValue">商品编码</returns>
        public ReturnValue GoodsDeleteCheck(string strGoodsID)
        {
            return dal_GOODS.GoodsDeleteCheck(strGoodsID);
        }

        /// <summary>
        /// <p><b>方法名称</b>CheckName</p>	
        /// <p><b>概要説明  :</b>商品名称的检索</p>
        /// <p><b>作成者    :</b>万岗</p>	
        /// <p><b>作成日    :</b>2008/09/17</p>	
        /// <p><b>修改历史  :</b></p>	
        /// </summary>	
        /// <param type="string" name="strGoodsID">商品名称</param>
        /// <returns type = "ReturnValue"></returns>
        public ReturnValue CheckName(MOD_M007_GOODS model)
        {
            return dal_GOODS.CheckName(model);
        }

        /// <summary>	
        /// <p><b>方法名称</b>InitCombox</p>	
        /// <p><b>概要説明  :</b>商品分类信息的初始化</p>	
        /// <p><b>作成者    :</b>万岗</p>	
        /// <p><b>作成日    :</b>2008/08/28</p>	
        /// <p><b>修改历史  :</b></p>	
        /// </summary>
        /// <returns type = "ReturnValue">执行结果表</returns>
        public ReturnValue InitCombox()
        {
            return dal_GOODS.Init_Combox();
        }

        /// <summary>	
        /// <p><b>方法名：</b>GetSingleData</p>	
        /// <p><b>概要説明  :</b>取得单条商品信息</p>	
        /// <p><b>作成者    :</b>万岗</p>	
        /// <p><b>作成日    :</b>2008/08/28</p>
        /// <p><b>修改历史  :</b></p>	
        /// </summary>	
        /// <param type="class" name="model">商品编号</param>
        /// <returns type = "ReturnValue">执行结果表</returns>
        public ReturnValue GetSingleData(MOD_M007_GOODS model)
        {
            return dal_GOODS.GetSingleData(model);
        }

        /// <summary>
        /// 根据商品条码查询商品
        /// </summary>
        /// <param name="GoodsNo">商品条码</param>
        /// <returns></returns>
        public ReturnValue GetSingleDataByNo(string GoodsNo)
        {
            return dal_GOODS.Get_Single_Data_ByNo(GoodsNo);
        }

        /// <summary>	
        /// <p><b>方法名称</b>SearchProcess</p>	
        /// <p><b>概要説明  :</b>商品信息的检索</p>	
        /// <p><b>作成者    :</b>万岗</p>	
        /// <p><b>作成日    :</b>2008/08/28</p>
        /// <p><b>修改历史  :</b></p>	
        /// </summary>	
        /// <param type="class" name="model">检索条件</param>
        /// <returns type = "ReturnValue">检索结果表</returns>
        public ReturnValue SearchProcess(MOD_M007_GOODS model)
        {
           return dal_GOODS.SearchProcess(model);
        }

        /// <summary>	
        /// <p><b>方法名称</b>InsertProcess</p>	
        /// <p><b>概要説明  :</b>商品信息的插入</p>	
        /// <p><b>作成者    :</b>万岗</p>	
        /// <p><b>作成日    :</b>2008/08/28</p>
        /// <p><b>修改历史  :</b></p>	
        /// </summary>	
        /// <param type="class" name="model">商品信息</param>
        /// <returns type = "ReturnValue">插入执行结果</returns>
        public string InsertProcess(MOD_M007_GOODS model)
        {
            ReturnValue returnValue ;

            #region 採番

            //MOD_M011_GET_ID modGetID = new MOD_M011_GET_ID();
            //modGetID.UPDATER_ID = model.UPDATER_ID;
            //modGetID.TERMINAL_CD = model.TERMINAL_CD;
            //modGetID.GETID_DIV = "02";
            //modGetID.FISCAL_YEAR = System.DateTime.Now.Year.ToString("####");
            //returnValue = bll_ComProcess.GetSlipID(modGetID);
            //if (!returnValue.ErrorFlag)
            //{
            //    return returnValue.ErrorID;
            //}
            //model.GOODS_ID = returnValue.ResultDataSet.Tables[0].Rows[0]["SLIPID"].ToString();

            string UniqueNo = string.Empty;
            if (!DAL_ComProcess.GetUniqueNo(model.UPDATER_ID
                , model.TERMINAL_CD
                , "02"
                , System.DateTime.Now.Year.ToString("####")
                , out UniqueNo))
            {
                return "E11003";
            }
            model.GOODS_ID = UniqueNo;

            #endregion

            returnValue = dal_GOODS.InsertProcess(model);
            if (returnValue.IsSuccess == false)
            {
                return returnValue.ErrorID;
            }
            return model.GOODS_ID;
        }

        /// <summary>
        /// 添加或更新商品信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ReturnValue AddOrUpdateGoods(MOD_M007_GOODS entity)
        {
            ReturnValue returnValue;

            if (entity.GOODS_ID != "")
            {
                //更新
                //首先检查非本ID但同名称的商品
                returnValue = dal_GOODS.CheckGoodsNameWithoutID(entity.GOODS_ID, entity.GOODS_NM1);
                if (!returnValue.IsSuccess)
                {
                    return returnValue;
                }
                if (returnValue.ResultDataSet.Tables[0].Rows.Count > 0)
                {
                    return new ReturnValue(false, "W11H01");//本[@]已经存在或已被逻辑删除!
                }

                //检查是否存在该ID的数据
                returnValue = dal_GOODS.CheckGoodsExist(entity.GOODS_ID);
                if (!returnValue.IsSuccess)
                {
                    return returnValue;
                }
                if (returnValue.ResultDataSet.Tables[0].Rows.Count < 1)
                {
                    return new ReturnValue(false, "W11N01");//[@]不存在！
                }
                //进行更新
                return dal_GOODS.UpdateProcess(entity);
            }
            else
            {
                //新添加
                //首先检查非本ID但同名称的商品
                returnValue = dal_GOODS.CheckGoodsNameWithoutID(entity.GOODS_ID, entity.GOODS_NM1);
                if (!returnValue.IsSuccess)
                {
                    return returnValue;
                }
                if (returnValue.ResultDataSet.Tables[0].Rows.Count > 0)
                {
                    return new ReturnValue(false, "W11H01");//本[@]已经存在或已被逻辑删除!
                }
                //***采用存储过程，进行自动采号，故不需要再进行采号
                //string UniqueNo = string.Empty;
                //if (!DAL_ComProcess.GetUniqueNo(entity.UPDATER_ID
                //    , entity.TERMINAL_CD
                //    , "07"
                //    , System.DateTime.Now.Year.ToString("####")
                //    , out UniqueNo))
                //{
                //    return new ReturnValue(false, "E11003");//生成业务编号失败
                //}
                //entity.GOODS_ID = UniqueNo;
                //进行添加
                returnValue = dal_GOODS.AddGoodsByProcedure(entity);
                if (!returnValue.IsSuccess)
                {
                    return returnValue;
                }
                return returnValue;
            }
        }

        /// <summary>	
        /// <p><b>方法名称</b>UpdateProcess</p>	
        /// <p><b>概要説明  :</b>商品信息的更新</p>	
        /// <p><b>作成者    :</b>万岗</p>	
        /// <p><b>作成日    :</b>2008/08/28</p>
        /// <p><b>修改历史  :</b></p>	
        /// </summary>	
        /// <param type="class" name="model">商品信息</param>
        /// <returns type = "ReturnValue">更新执行结果</returns>
        public string UpdateProcess(MOD_M007_GOODS model)
        {
            ReturnValue returnValue;
          
            returnValue = dal_GOODS.UpdateProcess(model);
            if (returnValue.IsSuccess == false)
            {
                return returnValue.ErrorID;
            }
            else
            {
                return "0";
            }
        }

        /// <summary>
        /// 逻辑删除商品
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ReturnValue DeleteProcessLogic(MOD_M007_GOODS model)
        {
            return dal_GOODS.LogicDeleteProcess(model);
        }
        /// <summary>
        /// 物理、彻底删除商品
        /// </summary>
        /// <param name="model"></param>
        public ReturnValue DeleteProcessPhysical(string GoodsID)
        {
            return dal_GOODS.PhysicalDeleteProcess(GoodsID);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ReturnValue UpdateGoodsUnits(MOD_M007_GOODS model)
        {
            return dal_GOODS.Update_Goods_Units(model);
        }

        public ReturnValue AddGoodsUnits(MOD_M007_GOODS model)
        {
            return dal_GOODS.Add_Goods_Units(model);
        }
    }
}
